<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IRequestAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';

	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];

	class MySqlRequestAccessDao implements IRequestAccessDao {  
		/**
		 * Constructor of the request dao
		 */
	 	public function __construct () {}
  
  		/**
		 * Selects all requests
		 */
		public function selectRequests($order) {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			switch ($order) {
				case 1:
					$query = "SELECT r.pk_number, r.pk_control_number, r.date, r.tax, 
								  r.read_status, lc.name client
							  FROM REQUEST r, LEGAL_CLIENT lc
							  WHERE r.type = 'PURCHASE'
							  AND r.fk_rif_legal_client IS NOT NULL
							  AND r.fk_rif_legal_client = lc.pk_rif
							  ORDER BY date DESC";
				break;
				
				case 2:
					$query = "SELECT r.pk_number, r.pk_control_number, r.date, r.tax, 
								  r.read_status, lc.name client
							  FROM REQUEST r, LEGAL_CLIENT lc
							  WHERE r.type = 'PRODUCTION'
							  AND r.fk_rif_legal_client IS NOT NULL
							  AND r.fk_rif_legal_client = lc.pk_rif
							  ORDER BY date DESC";
				break;
				
				case 3:
					$query = "SELECT r.pk_number, r.pk_control_number, r.date, r.tax, 
								  r.read_status, lc.name client
							  FROM REQUEST r, LEGAL_CLIENT lc
							  WHERE r.type = 'INSTALLATION'
							  AND r.fk_rif_legal_client IS NOT NULL
							  AND r.fk_rif_legal_client = lc.pk_rif
							  ORDER BY date DESC";
				break;
				
				case 4:
					$query = "SELECT r.pk_number, r.pk_control_number, r.date, r.tax, 
								  r.read_status, lc.name client
							  FROM REQUEST r, LEGAL_CLIENT lc
							  WHERE r.type = 'REQUISITION'
							  AND r.fk_rif_legal_client IS NOT NULL
							  AND r.fk_rif_legal_client = lc.pk_rif
							  ORDER BY date DESC";
				break;
				
				case 5:
					$query = "SELECT r.pk_number, r.pk_control_number, r.date, r.tax, 
								  r.read_status, lc.name client
							  FROM REQUEST r, LEGAL_CLIENT lc
							  WHERE r.type = 'OFFICE NOTE'
							  AND r.fk_rif_legal_client IS NOT NULL
							  AND r.fk_rif_legal_client = lc.pk_rif
							  ORDER BY date DESC";
				break;
			}
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
			
			return $sentence;
		 }
  	}
?>